# coding: utf-8
from PySide6.QtWidgets import QApplication

from common import SingleIcon, signalBus, cfg
from components import FluentWindowBase
from .first_window import FirstWindow
from .nuitka_window import NuiktaWindow
from .pyinstaller_window import PyInstallerWindow
from .setting_window import SettingWindow


class MainWindow(FluentWindowBase):
    def __init__(self):
        super().__init__()
        self.firstWindow = FirstWindow(self)
        self.pyInstallerWindow = PyInstallerWindow(self)
        self.nuitkaWindow = NuiktaWindow(self)
        self.settingWindow = SettingWindow(self)

        self.__initWidget()

    def __initWidget(self):
        self.setWindowTitle("PyToExe GUI")
        self.setWindowIcon(SingleIcon.Logo.icon())
        self.setMinimumSize(1000, 600)
        size = QApplication.primaryScreen().availableGeometry()
        self.move((size.width() - self.width()) // 2, (size.height() - self.height()) // 2)

        self.addWidget(self.firstWindow)
        self.addWidget(self.pyInstallerWindow)
        self.addWidget(self.nuitkaWindow)
        self.addWidget(self.settingWindow)
        self.switchTo(self.firstWindow)

        self.__connectSignals()

    def __connectSignals(self):
        self.firstWindow.typeChanged.connect(self.onTypeChanged)
        self.pyInstallerWindow.quitSignal.connect(self.onTypeChanged)
        self.nuitkaWindow.quitSignal.connect(self.onTypeChanged)
        self.settingWindow.quitSignal.connect(self.onTypeChanged)

        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)

    def onTypeChanged(self, tp: str = None):
        if tp == 'pyinstaller':
            self.switchTo(self.pyInstallerWindow)
            self.setWindowTitle('PyInstaller')
            self.setWindowIcon(SingleIcon.Pyinstaller.icon())
        elif tp == 'nuitka':
            self.switchTo(self.nuitkaWindow)
            self.setWindowTitle('Nuitka')
            self.setWindowIcon(SingleIcon.Nuitka.icon())
        elif tp == 'setting':
            self.switchTo(self.settingWindow)
            self.setWindowTitle('Setting')
            self.setWindowIcon(SingleIcon.Setting.icon())
        else:
            self.switchTo(self.firstWindow)
            self.setWindowTitle("PyToExe GUI")
            self.setWindowIcon(SingleIcon.Logo.icon())
